package sudoku.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;

/**
 * @author interfaz de usuario con menu al costado del tablero, incluye iconos
 * 
 */
public class SecondGui extends JFrame implements IView, Observer {

	private static final long serialVersionUID = 1L;
	private final Color CELESTE = new Color(75, 150, 250);
	private final Color CELESTE_BOTON = new Color(0, 0, 255);
	private final Color VERDE_BOTON = new Color(50, 205, 50);

	private JPanel contentPane;
	JTextField matrix[][] = new JTextField[9][9];
	private final int ALTO = 1;

	/**
	 * Create the frame.
	 */
	public SecondGui() {
		super("sudoku");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setSize(800, 600);
		setLocationRelativeTo(null);
		setVisible(true);
		// fin menu ayuda

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		// matriz
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		GridLayout grilla = new GridLayout(9, 9);
		panel.setLayout(grilla);

		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix.length; j++) {
				matrix[i][j] = new JTextField();
				Font f = new Font("TimesRoman", Font.BOLD, 36);
				matrix[i][j].setHorizontalAlignment((int) CENTER_ALIGNMENT);
				matrix[i][j].setFont(f);
				matrix[i][j].setCaretColor(Color.WHITE);// color del cursor
				matrix[i][j].setForeground(Color.WHITE);
				panel.add(matrix[i][j]);
			}
		}
		pintar_matrix();

		JToolBar toolBar = new JToolBar(JToolBar.VERTICAL);
		contentPane.add(toolBar, BorderLayout.WEST);

		JButton btnNuevoJuego = new JButton("Nuevo Juego", new ImageIcon(
				"src/sudoku/view/imagenes/1346283241_Play1Pressed.png"));
		btnNuevoJuego.setForeground(CELESTE_BOTON);
		btnNuevoJuego.setBorder(null);
		toolBar.add(btnNuevoJuego);

		JButton btnDetener = new JButton("Detener", new ImageIcon(
				"src/sudoku/view/imagenes/1346283247_button_blue_stop.png"));
		btnDetener.setForeground(VERDE_BOTON);
		btnDetener.setBorder(null);
		toolBar.add(btnDetener);

		toolBar.addSeparator();

		JButton btnPreferencias = new JButton(
				"preferencias",
				new ImageIcon(
						"src/sudoku/view/imagenes/1346283256_Gnome-Preferences-System-64.png"));
		btnPreferencias.setBorder(null);
		btnPreferencias.setForeground(CELESTE_BOTON);
		toolBar.add(btnPreferencias);

		toolBar.addSeparator();

		JButton btnResolver = new JButton("Resolver", new ImageIcon(
				"src/sudoku/view/imagenes/1346284546_kappfinder.png"));
		btnResolver.setBorder(null);
		btnResolver.setForeground(VERDE_BOTON);
		toolBar.add(btnResolver);

		JButton btnBorrar = new JButton("Borrar", new ImageIcon(
				"src/sudoku/view/imagenes/1346283340_clear.png"));
		btnBorrar.setBorder(null);
		btnBorrar.setForeground(CELESTE_BOTON);
		toolBar.add(btnBorrar);

		// JButton btnPista = new JButton("Pista");
		// toolBar.add(btnPista);
		//
		toolBar.addSeparator();

		JButton btnAyuda = new JButton(
				"Acerca de",
				new ImageIcon(
						"src/sudoku/view/imagenes/1346283259_Gnome-Dialog-Question-64.png"));
		btnAyuda.setBorder(null);
		btnAyuda.setForeground(VERDE_BOTON);
		toolBar.add(btnAyuda);

		JButton btnSalir = new JButton("Salir", new ImageIcon(
				"src/sudoku/view/imagenes/1346283265_button_cancel.png"));
		btnSalir.setBorder(null);
		btnSalir.setForeground(CELESTE_BOTON);
		toolBar.add(btnSalir);

		// fin botonera
	}

	private void pintar_matrix() {
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix.length; j++) {
				matrix[i][j].setBorder(BorderFactory
						.createLineBorder(Color.LIGHT_GRAY));
				matrix[i][j].setBackground(CELESTE);
			}
		}
		pintarCudrado(3, 0, this.ALTO);
		pintarCudrado(0, 3, this.ALTO);
		pintarCudrado(6, 3, this.ALTO);
		pintarCudrado(3, 6, this.ALTO);
	}

	private void pintarCudrado(int x, int y, int altura) {
		for (int i = x; i < x + 3; i++) {
			for (int j = y; j < y + 3; j++) {
				if (altura == this.ALTO) {
					matrix[i][j].setBorder(BorderFactory
							.createLineBorder(Color.WHITE));
					matrix[i][j].setBackground(Color.LIGHT_GRAY);
				} else
					matrix[i][j].setBorder(BorderFactory.createEtchedBorder());
			}
		}
	}

	public void dibujarNumeros() {

	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub

	}

	@Override
	public void present(String model) {
		JOptionPane.showMessageDialog(null, model);

	}

}
